<template>
    <div id="wgySwiper">
        <!-- ul的宽度等于数组的数量*100%-->
        <ul :style=" {
            width:100 * banners.length + '%',
            left:left,
            transition:transition
        } ">
            <li v-for="(item,i) in banners" :key="i" :style="{
                    width:100 / banners.length + '%'
                }"  @touchstart='touchStart'  @touchmove='touchMove' @touchend='touchEnd'>
        <!-- li的宽度等于100/数组的数量%-->

                <img :src="item.picUrl" alt="" >
            </li>
        </ul>

        <ol id="uu">
            <li>
                <img src="../../assets/img/qd.png" alt="">
                <p> 签到</p>
            </li>
            <li>
                <img src="../../assets/img/lq.png" alt="">
                <p> 礼券</p>
            </li>
            <li>
                <img src="../../assets/img/jj.png" alt="">
                <p> 砍价</p>
            </li>
            <li>
                <img src="../../assets/img/zl.png" alt="">
                <p> 专栏</p>
            </li>
        </ol>
    </div>
</template>

<script>
let setInt
export default {
    name: 'wgy-Swiper',
    props:{
        banners:{
            type:Array,
            // 传递对象或者数组类型的默认值必须用工厂函数
            default:() => {
                return []
            }
        },
        autoplay:{
            default:false
        }
    },
    data() {
        return {
            left:'-100%',
            transition:'none',
            direction:'left',
            startX:0,

        };
    },
    created() {
        if(this.autoplay !== false){

            setInt = setInterval(()=>{
                this.touchEnd()
            },3000)
        }
    },
    methods: {
        touchStart(e) {
            // console.log(e);
            // 获取点击位置的x坐标
            this.startX = e.touches[0].pageX    
            clearInterval(setInt)
            

        },
        touchMove(e) {
            // 获取移动方向
            let moveX = e.touches[0].pageX - this.startX
            if(moveX > 0){
                this.direction = 'right'
            } else if(moveX < 0){
                 this.direction = 'left'
            }
        },
        touchEnd() {
            let timer 

            this.transition = 'left .5s linear'
            clearTimeout(timer)

            if(this.direction == 'left'){
                this.left = '-200%'
                timer = setTimeout(() => {
                    let item = this.banners.shift()
                    this.banners.push(item)
                    this.transition = 'none'
                    this.left = '-100%'
                },500)
            }else if(this.direction == 'right'){
                 this.left = '0%'
                timer = setTimeout(() => {
                    let item = this.banners.pop()
                    this.banners.unshift(item)
                    this.transition = 'none'
                    this.left = '-100%'
                },500)
            }
        }
    },
    //组件挂载
    components: {},
    //计算属性
    computed: {},
    //过滤器
    filters: {},
};
</script>

<style lang="scss" scoped>
#wgySwiper{
    width: 100%;
    position: relative;
    ul{
       width: 375px;
       height:  375px;
        position: relative;
        li{
            width: 375px;
            height:  375px;
            overflow: hidden;
            float: left;
            img{
                width: 375px;
                height:  375px;
            }
        }
    }
    #uu{
        border-radius: 20% 20% 0 0;
        border-bottom: 0.2rem solid #F5F5F5;
        position: absolute;
        bottom: 0px;
        left: 0;
        width: 100%;
        height: 2.2rem;
        background-color: #fff;
        display: flex;
        justify-content: space-around;
        align-items: center;
        li{
            width: 25%;
            text-align: center;
            font-size: .36rem;
            display: list-item;
            text-align: -webkit-match-parent;
            img{
                width: 0.6rem;
                display: block;
                margin: 0 auto;
            }
            p{
                text-align: center;
                margin-top: 0.2rem;
            }
        }
    }
}
</style>
